<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/3/8
  Time: 16:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<style>
    .but{
        height: 20px;
        width:70px;
        cursor:pointer;
        background-color: rgb(0,150,136);
        display: inline-block;
        text-align: center;
        color: white;
        font-size: 12px;
        margin-left: 55px;
    }
</style>
<html>
<head>
    <title>添加用户</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webapp/css/layui.css">

</head>
<body>
    <div style="margin-left: 20%;margin-top: 8% ">
        <form class="layui-form" action="#" style="width: 500px">

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" id="username" lay-verify="required|phone" autocomplete="off" class="layui-input">
                    </div>
                </div>

            </div>
          <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="password" lay-verify="pass" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
            </div>

            <div id="roleWarp"style="margin-top: 10px">

            </div>

            <div style="margin-top: 10px">
                <div class="but" onclick="addUser()">提交</div>
                <div class="but" onclick="cancel()">取消</div>
            </div>
        </form>
    </div>

    <script type="text/javascript" src="${path}/webapp/js/jquery-3.1.1.js"></script>

<script type="text/javascript">
   $(function () {
       $.ajax({
           url:'${path}/sys/admin/getAllRole',
           dataType:'json',
           type: 'get',
           success:function (data) {
              for (var i=0;i<data.length;i++){
                  $("#roleWarp").append("<div  style='display: inline-block;font-size: 15px;margin-left: 80px;text-align: center'>\n" +
                      "                    <input type=\"checkbox\" value="+data[i].rid+" class=\"role\" style='display: inline-block;height: 17px;width: 17px;margin-right: 3px'>"+data[i].roleNameCn+"\n" +
                      "                </div>")
              }
           },
           error:function () {

           }

       })
   })
   //添加用户
    function addUser() {
        var username=$("#username").val();
        var password=$("#password").val();
        var roleId=new Array();


        $("input[class='role']:checked").each(function(i){
            roleId[i] = $(this).val();
        });
        var falg=true;
        falg=isNotNull(username);
        falg=isNotNull(password);
        falg=isNotNull(roleId);
        if(falg){
            $.ajax({
                url:'${path}/sys/admin/addUser',
                data:{'username':username,
                    'password':password,
                    'roleId':roleId
                },
                dataType:'json',
                traditional:true,
                type:'post',
                success:function (data) {
                    if(data==1){
                        alert("添加成功");
                        //window.parent.location.reload();
                        window.close();
                        window.parent.loadHtml('${path}/sys/admin/intoUser')

                    }else if(data=2){
                        alert("用户名已存在");
                    }else{
                        alert("服务器忙");
                    }
                },
                error:function () {
                    alert("服务器忙");
                }
            })
        }else{
            alert("请填写完整")
        }
    }

//取消
    function cancel() {

        window.close();
        window.parent.location.reload();
    }
   function isNotNull(ele){
       if(typeof ele==='undefined'){//先判断类型
           return false;
       }else if(ele==null){
           return false;
       }else if(ele==''){
           return false;
       }
       return true;
   }
</script>
</body>
</html>
